package com.raincat.tools.nettools;

import android.content.Context;

import androidx.annotation.NonNull;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * <pre>
 *     author : RainCat
 *     org    : Shenzhen JingYu Network Technology Co., Ltd.
 *     e-mail : nining377@gmail.com
 *     time   : 2020/03/23
 *     desc   : 上传配置
 *     version: 1.0
 * </pre>
 */
public class UploadConfig {
    static final String UPLOAD = "UPLOAD";

    private Request request;
    private ConnectUpload connectUpload;

    public UploadConfig() {
        request = new Request();
        request.requestMode = UPLOAD;
        request.cookie = NetConfig.cookie;
        request.reTry = NetConfig.reTry;
        request.connectTimeout = NetConfig.connectTimeout;
        request.readTimeout = NetConfig.readTimeout;
    }

    public UploadConfig doUpload(@NonNull String url, @NonNull String fileKey, HashMap<String, Object> params, @NonNull ArrayList<String> filePathList) {
        request.params = params;
        request.fileKey = fileKey;
        request.filePathList = filePathList;
        request.url = url;
        return this;
    }

    /**
     * 请求头
     *
     * @param headers 请求头
     * @return 返回值
     */
    public UploadConfig headers(@NonNull HashMap<String, Object> headers) {
        request.headers = headers;
        return this;
    }

    /**
     * 是否显示加载框
     *
     * @param dialogMessage 加载框信息
     * @return 返回值
     */
    public UploadConfig showLoadingDialog(@NonNull String dialogMessage) {
        request.dialogMessage = dialogMessage;
        return this;
    }

    public void start(Context context, NetCallBack back) {
        if (request.url == null || request.url.length() == 0)
            throw new IllegalArgumentException("url can't be null");
        request.context = new WeakReference<>(context);
        request.back = back;
        connectUpload = new ConnectUpload();
        connectUpload.init(request);
    }

    public void cancel() {
        connectUpload.cancel();
    }
}
